<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>列车管理</title>
    <script type="text/javascript">
      var newTrainWin;
      var editTrainWin;
      var editTrainRouterWin;
      var trainId;
      Ext.onReady(function(){
        //列车管理---------------------------------------------------------
        //生成有关列车的Grid
        var trainDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize
          },
          proxy: new Ext.data.HttpProxy({
            url: '/train/getTrains'
          }),
          reader:new Ext.data.JsonReader({
            totalProperty: 'total',
            root:'train'
          },[
            {name:'id'},
            {name:'sn'},
            {name:'date'},
            {name:'last_length'},
            {name:'now_length'},
            {name:'all_length'},
            {name:'xx_length'},
            {name:'ts_length'},
            {name:'two_level_fix_length'},
            {name:'threex_length'},
            {name:'fourx_length'},
            {name:'line'},
            {name:'train_number'},
            {name:'length'},
            {name:'is_locked'}
          ]
        )
        });

        var trainSM = new Ext.grid.CheckboxSelectionModel();
        var trainCM = new Ext.grid.ColumnModel([
          new Ext.grid.RowNumberer({width: 30}),trainSM,
          {
            header: '是否锁定',
            dataIndex: 'is_locked',
            renderer: renderIsLucked,
            width: 70
          },
          {header:'车列',sortable:true,dataIndex:'sn',width:110},
          {header:'担当车次',sortable:true,dataIndex:'train_number',width:160},
          {header:'运行交路',sortable:true,dataIndex:'line',width:220},
          {header:'上次走行<br>  公里',sortable:true,dataIndex:'last_length',width:70},
          {header:'本次走行<br>  公里',sortable:true,dataIndex:'now_length',width:70},
          {header:'总走行公<br>  里数',sortable:true,dataIndex:'all_length',width:70},
          {header:'镟修后<br>公里数',sortable:true,dataIndex:'xx_length',width:60},
          {header:'探伤后<br>公里数',sortable:true,dataIndex:'ts_length',width:60},
          {header:'二级修<br>公里数',sortable:true,dataIndex:'two_level_fix_length',width:60},
          {header:'三级修<br>后公里',sortable:true,dataIndex:'threex_length',width:60},
          {header:'四级修<br>后公里',sortable:true,dataIndex:'fourx_length',width:60},
          {header:'一次走<br>行公里',sortable:true,dataIndex:'length',width:60}
        ]);

        var trainGrid =  new Ext.grid.GridPanel({
          ds: trainDS,
          cm: trainCM,
          sm: trainSM,
          loadMask: true,
          width: screenWidth-240,
          height: screenHeight-230,
          iconCls: 'icon-grid',
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize,
            store: trainDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          }),
          tbar: ['-',{
              text: '添加列车',
              width:50,
              iconCls: 'addItem',
              handler : function(){
                newTrainWin = new Ext.Window({
                  layout: 'fit',
                  width:397,
                  height:455,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  title: '添加列车',
                  autoLoad: {
                    url: "/train/newTrain",
                    scripts: true
                  }
                });
                newTrainWin.on("destroy",function(){
                  trainDS.reload();
                });
                newTrainWin.resizable = false;
                newTrainWin.show();
              }
            },{
              text: '编辑列车信息',
              width:50,
              iconCls: 'editItem',
              handler : function(){
                var records = trainGrid.getSelectionModel().getSelections();
                if(records.length==0){
                  Ext.MessageBox.alert('提示信息', '请先选中一条记录后，再编辑。');
                }else{
                  //把表单添加到窗口中
                  trainId = records[0].get('id');
                  editTrainWin = new Ext.Window({
                    title: '编辑列车',
                    layout:'fit',
                    width:397,
                    height:455,
                    closeAction:'destroy',
                    constrain:true,
                    plain: true,
                    modal: true,
                    autoLoad: {
                      url: "/train/editTrain?id=" + trainId,
                      scripts: true
                    }
                  });
                  editTrainWin.on("destroy",function(){
                    trainDS.reload();
                  });
                  editTrainWin.resizable = false;
                  editTrainWin.show();
                }
              }
            },
            {
              text:'锁定或解锁列车',
              width:80,
              iconCls: 'remove',
              handler:function(){
                if(trainGrid.getSelectionModel().hasSelection()){
                  var rs=trainGrid.getSelectionModel().getSelections();
                  var ids=[];
                  var trainName='';
                  for(var i=0;i<rs.length;i++){
                    var data=rs[i].data;
                    ids.push(data.id);
                    trainName+=data.sn+'<br>';
                  }
                  var keys=Ext.util.JSON.encode(ids);
                  Ext.Ajax.request({
                    url:'/train/doLock',
                    method:'post',
                    waitMsg: '正在锁定列车...',
                    success:function(response,opts){
                      var data = Ext.util.JSON.decode(response.responseText);
                      if(data.success&&data.info=='success') {
                        trainDS.load();
                      } else {
                        Ext.Msg.alert('提示信息', data.info);
                      }
                    },
                    params: {"id":keys}
                  });
                }else{
                  Ext.Msg.alert('提示信息','请选择要删除的列车');
                }
              }
            },{
              text: '设置列车交路模式',
              width:50,
              iconCls: 'editItem',
              handler : function(){
                var records = trainGrid.getSelectionModel().getSelections();
                if(records.length==0){
                  Ext.MessageBox.alert('提示信息', '请先选中一条记录后，再设置。');
                }else{
                  //把表单添加到窗口中
                  trainId = records[0].get('id');
                  editTrainRouterWin = new Ext.Window({
                    title: '设置列车交路模式',
                    layout:'fit',
                    width:542,
                    height:240,
                    closeAction:'destroy',
                    constrain:true,
                    plain: true,
                    modal: true,
                    autoLoad: {
                      url: "/train/editTrainRouter?trainId=" + trainId,
                      scripts: true
                    }
                  });
                  editTrainRouterWin.on("destroy",function(){
                    trainDS.reload();
                  });
                  editTrainRouterWin.resizable = false;
                  editTrainRouterWin.show();
                }
              }
            }]
        });

        trainGrid.on('rowcontextmenu', function(grid,rowindex,e){               //用于生成右键菜单
          trainSn = trainDS.getAt(rowindex).get('sn')                      //在这里取得值，可以在后面的function中使用
          trainId = trainDS.getAt(rowindex).get('id')
          e.preventDefault();
          rightClick.showAt(e.getXY());
        });

        var rMenu0 = new Ext.menu.Item({
          text:'列车停留',
          handler:function(){
            Ext.MessageBox.confirm('警告','确定使列车<font color="red">'+trainSn
              + '</font>停留？<br>',function (button){
              if(button=='yes'){
                Ext.Ajax.request({
                  url:'/train/setStay',
                  method:'post',
                  success:function(response,opts){
                    var data = Ext.util.JSON.decode(response.responseText);
                    if(data.success&&data.info=='success') {
                      Ext.Msg.alert('提示信息', '列车已经设置为停留');
                    } else {
                      Ext.Msg.alert('提示信息', data.info);
                    }
                  },
                  params: {"trainId":trainId}
                });
                trainDS.reload();
              }
            })
          },
          iconCls:'viewItem'
        })

        var rMenu1 = new Ext.menu.Item({
          text:'进行镟修',
          handler:function(){
            newTrainTaskWin = new Ext.Window({
              layout: 'fit',
              width:797,
              height:548,
              closeAction: 'destroy',
              plain: true,
              modal: true,
              constrain:true,
              title: '添加镟修任务',
              autoLoad: {
                url: "/daily/xxTask?trainId="+trainId+"&trainSn="+trainSn,
                scripts: true
              }
            });
            newTrainTaskWin.on("destroy",function(){
              trainDS.reload();
            });
            newTrainTaskWin.resizable = false;
            newTrainTaskWin.show();
          },
          iconCls:'viewItem'
        })

        var rMenu2 = new Ext.menu.Item({
          text:'进行探伤',
          handler:function(){
            newTrainTaskWin = new Ext.Window({
              layout: 'fit',
              width:797,
              height:548,
              closeAction: 'destroy',
              plain: true,
              modal: true,
              constrain:true,
              title: '添加探伤任务',
              autoLoad: {
                url: "/daily/tsTask?trainId="+trainId+"&trainSn="+trainSn,
                scripts: true
              }
            });
            newTrainTaskWin.on("destroy",function(){
              trainDS.reload();
            });
            newTrainTaskWin.resizable = false;
            newTrainTaskWin.show();
          },
          iconCls:'viewItem'
        })

        var rMenu3 = new Ext.menu.Item({
          text:'进行三级修',
          handler:function(){
            Ext.MessageBox.confirm('警告','确定对<font color="red">'+trainSn
              + '</font>进行三级修？<br>（本操作不可恢复）',function (button){
              if(button=='yes'){
                Ext.Ajax.request({
                  url:'/train/threex',
                  method:'post',
                  success:function(response,opts){
                    var data = Ext.util.JSON.decode(response.responseText);
                    if(data.success&&data.info=='success') {
                      Ext.Msg.alert('提示信息', '三级修后公里数已经清零');
                    } else {
                      Ext.Msg.alert('提示信息', data.info);
                    }
                  },
                  params: {"trainId":trainId}
                });
                trainDS.reload();
              }
            })
          },
          iconCls:'viewItem'
        })

        var rMenu4 = new Ext.menu.Item({
          text:'进行四级修',
          handler:function(){
            Ext.MessageBox.confirm('警告','确定对<font color="red">'+trainSn
              + '</font>进行四级修？<br>（本操作不可恢复）',function (button){
              if(button=='yes'){
                Ext.Ajax.request({
                  url:'/train/fourx',
                  method:'post',
                  success:function(response,opts){
                    var data = Ext.util.JSON.decode(response.responseText);
                    if(data.success&&data.info=='success') {
                      Ext.Msg.alert('提示信息', '四级修后公里数已经清零');
                    } else {
                      Ext.Msg.alert('提示信息', data.info);
                    }
                  },
                  params: {"trainId":trainId}
                });
                trainDS.reload();
              }
            })
          },
          iconCls:'viewItem'
        })

        var rightClick = new Ext.menu.Menu({
          id:'rightClickCont',
          items: [rMenu0,rMenu1,rMenu2,rMenu3,rMenu4]
        });


        trainGrid.render('trainList');
        trainDS.load();
      })

    </script>
  </head>
  <body>
    <div id="trainList"></div>
  </body>
</html>
